<template>
  <view class="page">
    <!-- 页头导航 -->
    <view class="header-nav">
      <view class="nav-left" @tap="goBack">
        <image src="/static/icons/left_arrow.png" class="back-icon"></image>
      </view>
      <text class="nav-title">选择您当前就职的公司</text>
      <view class="nav-right"></view>
    </view>

    <view class="container">
      <!-- 输入框区域 -->
      <view class="input-section">
        <view class="input-wrapper">
          <input
            v-model="companyName"
            class="company-input"
            placeholder="请输入营业执照上的公司名称"
            maxlength="30"
            @input="onInput"
          />
          <view class="char-count">{{ companyName.length }}/30</view>
        </view>
      </view>

      <!-- 注意事项 -->
      <view class="notice-section">
        <text class="notice-title">注意事项</text>
        <view class="notice-list">
          <text class="notice-item">1. 请务必输入营业执照上的公司名称</text>
          <text class="notice-item">2. 请注意区分总公司和分公司</text>
          <text class="notice-item"
            >3. 公司名称会用于后续的企业认证，请确保正确</text
          >
        </view>
      </view>

      <!-- 营业执照图片展示 -->
      <view class="license-section">
        <text class="license-title">营业执照示例</text>
        <image
          src="/static/images/company/BusinessLicence.png"
          class="license-image"
          mode="widthFix"
        ></image>
      </view>

      <!-- 确认选择按钮 -->
      <button
        class="confirm-btn"
        @tap="confirmSelection"
        :disabled="!companyName.trim()"
      >
        确认选择
      </button>
    </view>
  </view>
</template>

<script setup>
import { ref } from "vue";
import { getCompanyInfoApi } from "@/api/company.js";

const companyName = ref("");
const isLoading = ref(false);

// 返回上一页
const goBack = () => {
  uni.navigateBack();
};

// 输入处理
const onInput = (e) => {
  companyName.value = e.detail.value;
};

// 确认选择
const confirmSelection = async () => {
  if (!companyName.value.trim()) {
    uni.showToast({
      title: "请输入公司名称",
      icon: "none",
    });
    return;
  }

  try {
    isLoading.value = true;
    uni.showLoading({
      title: "查询中...",
      mask: true,
    });

    const res = await getCompanyInfoApi(companyName.value.trim());

    uni.hideLoading();
    isLoading.value = false;

    if (!res || !res.id) {
      // 公司不存在，询问用户是否入驻
      uni.showModal({
        title: "公司未找到",
        content: "该公司尚未入驻平台，您是否要申请成为该公司的HR？",
        confirmText: "申请入驻",
        cancelText: "重新输入",
        success: (res) => {
          if (res.confirm) {
            // 用户选择入驻，跳转到创建公司页面
            uni.navigateTo({
              url: `/pages/candidate/createCompany?companyName=${encodeURIComponent(companyName.value.trim())}`
            })
          }
        },
      });
    } else {
      // 公司存在，跳转到公司授权加入页面，传递公司ID
      uni.navigateTo({
        url: `/pages/candidate/authorizeJoin?companyId=${res.id}`
      });
    }
  } catch (error) {
    uni.hideLoading();
    isLoading.value = false;
    uni.showToast({
      title: "查询失败，请重试",
      icon: "none",
    });
  }
};

// 返回上一页并传递数据
const returnToPreviousPage = (companyInfo) => {
  const pages = getCurrentPages();
  const prevPage = pages[pages.length - 2];

  if (prevPage && prevPage.$vm) {
    prevPage.$vm.setCompanyInfo(companyInfo);
  }

  uni.navigateBack();
};

// 生成公司ID（模拟）
const generateCompanyId = (name) => {
  // 这里应该调用API获取真实公司ID
  // 暂时使用简单的hash模拟
  let hash = 0;
  for (let i = 0; i < name.length; i++) {
    const char = name.charCodeAt(i);
    hash = (hash << 5) - hash + char;
    hash = hash & hash; // 转换为32位整数
  }
  return Math.abs(hash).toString();
};
</script>

<style scoped>
.page {
  height: 100vh;
  background-color: #f8f8f8;
  display: flex;
  flex-direction: column;
}

.header-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 88rpx;
  background-color: #ffffff;
  padding: 0 30rpx;
  border-bottom: 1rpx solid #f0f0f0;
}

.nav-left {
  display: flex;
  align-items: center;
  width: 60rpx;
}

.back-icon {
  width: 32rpx;
  height: 32rpx;
}

.nav-title {
  font-size: 34rpx;
  font-weight: 500;
  color: #333333;
}

.nav-right {
  width: 60rpx;
}

.container {
  flex: 1;
  padding: 40rpx;
  overflow-y: auto;
}

.input-section {
  margin-bottom: 60rpx;
}

.input-wrapper {
  position: relative;
  border-bottom: 2rpx solid #e0e0e0;
  padding-bottom: 20rpx;
}

.company-input {
  width: 100%;
  font-size: 32rpx;
  color: #333333;
  padding: 20rpx 0;
  border: none;
  outline: none;
  background: transparent;
}

.company-input::placeholder {
  color: #999999;
}

.char-count {
  position: absolute;
  right: 0;
  bottom: 20rpx;
  font-size: 24rpx;
  color: #999999;
}

.notice-section {
  margin-bottom: 60rpx;
}

.notice-title {
  display: block;
  font-size: 32rpx;
  font-weight: 600;
  color: #333333;
  margin-bottom: 30rpx;
}

.notice-list {
  display: flex;
  flex-direction: column;
  gap: 20rpx;
}

.notice-item {
  font-size: 28rpx;
  color: #666666;
  line-height: 1.6;
}

.license-section {
  margin-bottom: 80rpx;
  text-align: center;
}

.license-title {
  display: block;
  font-size: 32rpx;
  font-weight: 600;
  color: #333333;
  margin-bottom: 30rpx;
}

.license-image {
  width: 100%;
  max-width: 600rpx;
  border-radius: 16rpx;
  box-shadow: 0 4rpx 20rpx rgba(0, 0, 0, 0.1);
}

.confirm-btn {
  width: 100%;
  height: 88rpx;
  background-color: #31b9b3;
  color: #ffffff;
  font-size: 32rpx;
  border: none;
  border-radius: 12rpx;
  margin-top: auto;
}

.confirm-btn:disabled {
  background-color: #cccccc;
  color: #ffffff;
}

.confirm-btn:active {
  opacity: 0.8;
}
</style>